package server.listeners;


import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import commons.Manager;

import upload.Server;

/**
 * Class used once on webapp startup and on webapp destroy
 * @author Romain Pellerin
 */
public class AppListener implements ServletContextListener {

	private static Server serv;
	
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		Manager.getInstance(); // starts the GUI
		if (serv==null || !serv.isAlive())
			(serv = new Server()).start(); // starts the upload server
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		try {
			Manager.getInstance().kill();
		}
		catch(Exception e) {/*ignore*/}
		if (serv != null && serv.isAlive()) {
			serv.killSocket();
		}
	}
}
